package org.zkoss.zktc.core.widget.impl;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;
import org.zkoss.zktc.core.widget.SelectWidget;

public class SelectWidgetImpl extends WidgetImpl implements SelectWidget {

	public SelectWidgetImpl(WidgetDriverImpl driver, String gadgetId,boolean autoReattach) {
		super(driver,gadgetId, autoReattach);
	}
	
	Select toSelect(){
		Select select = new Select(getRealDom());
		return select;
	}

	public SelectWidget select(int index) {
		return select(index,1,driver.getTimeout());
	}

	public SelectWidget select(int index, int trip, long timeout) {
		Select s = toSelect();
		
		long tid = driver.getTripId();
		if(index<0){
			s.deselectAll();
		}else{
			s.selectByIndex(index);
		}
		driver.waitForTrip(tid,trip, timeout);
		return this;
	}
	
	public SelectWidget deselect(int index) {
		return select(index,1,driver.getTimeout());
	}

	public SelectWidget deselect(int index, int trip, long timeout) {
		Select s = toSelect();
		
		long tid = driver.getTripId();
		if(index<0){
			s.deselectAll();
		}else{
			s.deselectByIndex(index);
		}
		driver.waitForTrip(tid,trip, timeout);
		return this;
	}

}
